package service;


import entity.SysUser;
import mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import shili.server.SysUserService;

import java.util.List;

@Transactional
@Service("sysUserService")
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<SysUser> getList(SysUser sysUser) {
        try {
            return sysUserMapper.selectSysUserList(sysUser);
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw e;
        }
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public boolean add(SysUser sysUser) {
        // 模拟在一个事务中两次对数据库的操作，第一次操作完成之后发生异常时，第二次未操作未执行，第一次操作也将回滚。两次操作要么都不成功，要么同时成功

        int num = sysUserMapper.add(sysUser); // 模拟程序中抛出异常
        // if (sysUser != null) {
        // 手动抛出异常
        // throw new SendEMailException("发送通知邮件异常");
        // }
        if (num > 0){
            return true;
        }else {
            return false;
        }

    }
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public int delete(Integer id) {
        return sysUserMapper.delete(id);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public int updateUser(SysUser sysUser) {
        return sysUserMapper.updateUser(sysUser);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<SysUser> CheckUser(Integer roleId) {
        return sysUserMapper.CheckUser(roleId);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public int deleteByRoleId(Integer roleId) {
        return sysUserMapper.deleteByRoleId(roleId);
    }

}
